#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @license : (C) Copyright 2017-2020, ZGMicro.com.
# @Time    : 2020/5/23 22:38
# @File    : backtrader3.py
# @Software: PyCharm
# @desc    :
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import datetime
import backtrader as bt  # 引入backtrader框架

import tushare as ts
import pandas as pd

from datetime import datetime

import os, sys


# 平滑异同移动平均线MACD
# 买入与卖出算法：
#   macd、signal、histo都大于0，买入
#   macd、signal、histo都小于等于0，卖出


class StrategyClass(bt.Strategy):
    '''#平滑异同移动平均线MACD
        DIF(蓝线): 计算12天平均和26天平均的差，公式：EMA(C,12)-EMA(c,26)
       Signal(DEM或DEA或MACD) (红线): 计算macd9天均值，公式：Signal(DEM或DEA或MACD)：EMA(MACD,9)
        Histogram (柱): 计算macd与signal的差值，公式：Histogram：MACD-Signal
        period_me1=12
        period_me2=26
        period_signal=9
        macd = ema(data, me1_period) - ema(data, me2_period)
        signal = ema(macd, signal_period)
        histo = macd - signal
    '''

    def __init__(self):
        # sma源码位于indicators\macd-B.py
        # 指标必须要定义在策略类中的初始化函数中
        macd = bt.ind.MACD()
        self.macd = macd.macd
        self.signal = macd.signal
        self.histo = bt.ind.MACDHisto()

        self.dataclose = self.datas[0].close

        self.order = None
        self.buyprice = None
        self.buycomm = None

    def log(self, txt, dt=None):
        ''' Logging function for this strategy'''
        dt = dt or self.datas[0].datetime.date(0)
        print('%s, %s' % (dt.isoformat(), txt))

    def notify_cashvalue(self, cash, value):
        self.log('Cash %s Value %s' % (cash, value))

    def notify_order(self, order):
        print(type(order), 'Is Buy ', order.isbuy())
        if order.status in [order.Submitted, order.Accepted]:
            # Buy/Sell order submitted/accepted to/by broker - Nothing to do
            return

        # Check if an order has been completed
        # Attention: broker could reject order if not enough cash
        if order.status in [order.Completed]:
            if order.isbuy():
                self.log(
                    'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                    (order.executed.price,
                     order.executed.value,
                     order.executed.comm))

                self.buyprice = order.executed.price
                self.buycomm = order.executed.comm
            else:  # Sell
                self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                         (order.executed.price,
                          order.executed.value,
                          order.executed.comm))

            self.bar_executed = len(self)


        elif order.status in [order.Canceled, order.Margin, order.Rejected]:
            self.log('Order Canceled/Margin/Rejected')

        self.order = None

    def notify_trade(self, trade):
        if not trade.isclosed:
            return

        self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %
                 (trade.pnl, trade.pnlcomm))

    def next(self):

        if self.order:  # 检查是否有指令等待执行,
            return

        # Simply log the closing price of the series from the reference
        self.log('Close, %.2f' % self.dataclose[0])
        # Check if we are in the market
        if not self.getposition(self.datas[0]):

            # self.data.close是表示收盘价
            # 收盘价大于histo，买入
            if self.macd > 0 and self.signal > 0 and self.histo > 0:
                self.log('BUY CREATE,{}'.format(self.dataclose[0]))
                self.order = self.buy(self.datas[0])

        else:

            # 收盘价小于等于histo，卖出
            if self.macd <= 0 or self.signal <= 0 or self.histo <= 0:
                self.log('BUY CREATE,{}'.format(self.dataclose[0]))
                self.log('Pos size %s' % self.position.size)
                self.order = self.sell(self.datas[0])


def get_data(code, start='2018-01-01', end='2020-05-22'):
    df = ts.get_k_data(code, autype='qfq', start=start, end=end)
    df.index = pd.to_datetime(df.date)
    df['openinterest'] = 0
    df = df[['open', 'high', 'low', 'close', 'volume', 'openinterest']]
    return df


def bt3():
    start = datetime(2017, 1, 1)
    end = datetime(2020, 5, 1)
    k_data = get_data('300676', start.strftime("%Y%m%d"), end.strftime("%Y%m%d"))

    # Create a Data Feed
    data = bt.feeds.PandasData(dataname=k_data,
                               fromdate=start,
                               todate=end)

    return data


cerebro = bt.Cerebro()

cerebro.adddata(bt3())

cerebro.addstrategy(StrategyClass)
# 设置金额，默认是200000
cerebro.broker.set_cash(200000)
cerebro.run(maxcpu=1)
cerebro.plot()
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())